package com.lin.operatorlogrecord.controller;

import com.lin.operatorlogrecord.entity.annotation.OperatorLogAnnotation;
import com.lin.operatorlogrecord.entity.dto.UserRolesDTO;
import com.lin.operatorlogrecord.entity.enums.LogTypeEnum;
import com.lin.operatorlogrecord.entity.model.Role;
import com.lin.operatorlogrecord.entity.model.User;
import com.lin.operatorlogrecord.mapper.UserMapper;
import com.lin.operatorlogrecord.strategy.AddBatchSubObjOperatorLogStrategy;
import com.lin.operatorlogrecord.strategy.AddOperatorLogStrategy;
import com.lin.operatorlogrecord.strategy.DeleteOperatorLogStrategy;
import com.lin.operatorlogrecord.strategy.UpdateOperatorLogStrategy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: chenghao.lin
 * @Date: 2023/09/21
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;


    @PostMapping
    @OperatorLogAnnotation(requestObjSpel = "#user", iOperatorLogStrategy = AddOperatorLogStrategy.class,
        tableEntity = User.class, logType = LogTypeEnum.USER_MANAGE)
    public String create(@RequestBody User user) {
        userMapper.insert(user);
        return user.getObjId();
    }

    @DeleteMapping("/{id}")
    @OperatorLogAnnotation(operatorObjIdSpel = "#id", iOperatorLogStrategy = DeleteOperatorLogStrategy.class,
            tableEntity = User.class, logType = LogTypeEnum.USER_MANAGE)
    public void delete(@PathVariable("id") String id) {
        userMapper.deleteById(id);
    }

    @PutMapping
    @OperatorLogAnnotation(requestObjSpel = "#user", operatorObjIdSpel = "#user.getObjId()", iOperatorLogStrategy = UpdateOperatorLogStrategy.class,
            tableEntity = User.class, logType = LogTypeEnum.USER_MANAGE)
    public void update(@RequestBody User user) {
        userMapper.updateById(user);
    }

    @PatchMapping("/addRolse")
    @OperatorLogAnnotation(requestObjSpel = "#userRolesDTO.getRoleIds()", operatorObjIdSpel = "#userRolesDTO.getUserId()",
            iOperatorLogStrategy = AddBatchSubObjOperatorLogStrategy.class,
            tableEntity = {User.class, Role.class}, logType = LogTypeEnum.USER_MANAGE)
    public void updateRoles(@RequestBody UserRolesDTO userRolesDTO) {

    }

}
